package com.zhou.util;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

public class PropertyFileReader {

	Properties properties  = new Properties();


	public  void load(String fileName) throws  IOException {
		if (StringUtils.isEmpty(fileName)){
			throw  new IOException("fileName is Empty.");
		}
		System.out.println("PropertyFileReader load fileName="+fileName);
		load(PropertyFileReader.class.getClassLoader().getResourceAsStream(fileName));
	}
	public void load(InputStream inputStream) throws  IOException {
		properties.load(inputStream);
	}

	public String getString(String key) {
		Object value = properties.get(key);
		if (value == null){
			return null;
		}
		return value.toString();
	}

	public  String getString(String key, String defaultValue) {
		String value = getString(key);
		return StringUtils.isNotBlank(value) ? value : defaultValue;
	}

	public  int getInt(String key, int defaultValue) {
		String value = getString(key);
		return StringUtils.isNotBlank(value)  ? Integer.parseInt(value) : defaultValue;
	}


	public int getInt(String key) {
		return getInt(key,0);
	}


	public long getLong(String key) {
		return getLong(key,0l);
	}
	public long getLong(String key,long defaultValue) {
		String value = getString(key);
		return StringUtils.isNotBlank(value)  ? Long.parseLong(value) : defaultValue;
	}

	public boolean getBooolean(String key) {
		return  getBooolean(key,false);
	}

	public boolean getBooolean(String key,boolean defaultValue) {
		String value = getString(key);
		return StringUtils.isNotBlank(value)  ? value.toLowerCase().equals("true") : defaultValue;
	}

	public void setProperties(Properties properties) {
		this.properties = properties;
	}

	public Properties getProperties() {
		return properties;
	}

	public void put(String key, Object value) {
		properties.put(key,value);
	}
}
